package com.geetour.columbus.region.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geetour.columbus.common.exceptions.ServiceException;
import com.geetour.columbus.common.model.R.ProvincePage;
import com.geetour.columbus.common.service.IProvincePageService;
import com.geetour.columbus.region.bo.IProvincePageBO;
import com.geetour.columbus.region.service.IProvincePageInnerService;
import com.geetour.columbus.region.utils.ProvincePageUtil;

@Service
public class ProvincePageServiceImpl implements IProvincePageService, IProvincePageInnerService {

    private IProvincePageBO provincePageBOImpl;

    @Autowired
    public void setProvincePageBOImpl(IProvincePageBO provincePageBOImpl) {
        this.provincePageBOImpl = provincePageBOImpl;
    }

    @Override
    public void insertProvincePage(ProvincePage province) throws ServiceException {
        if (province == null || province.getName() == null) {
            return;
        }
        provincePageBOImpl.insertProvincePage(province);
    }

    @Override
    public int insertProvincePageNames(List<String> names) throws ServiceException {
        if (names == null || names.isEmpty()) {
            return -1;
        }
        List<String> insertNames = new ArrayList<String>(); 
        for (String name : names) {
            ProvincePage result = provincePageBOImpl.findProvincePageByName(name);
            if (result == null) {
                insertNames.add(name);
            }
        }
        List<ProvincePage> provList = null;
        if (insertNames != null && !insertNames.isEmpty()) {
            provList = ProvincePageUtil.getProvinceByNames(insertNames);
            return provincePageBOImpl.insertProvincePageBatch(provList);
        }
        return -1;
    }

    @Override
    public int updateProvincePage(ProvincePage province) throws ServiceException {
        if (province == null) {
            return -1;
        }
        return provincePageBOImpl.updateProvincePage(province);
    }

    @Override
    public int deleteProvincePageById(Long id) throws ServiceException {
        if (id == null) {
            return -1;
        }
        return provincePageBOImpl.deleteProvincePage(id);
    }

    @Override
    public ProvincePage findProvincePageById(Long id) throws ServiceException {
        if (id == null) {
            return null;
        }
        return provincePageBOImpl.findProvincePageById(id);
    }

    @Override
    public List<ProvincePage> findAllProvincePages() throws ServiceException {
        return provincePageBOImpl.findAllProvincePages();
    }

    @Override
    public List<ProvincePage> findProvincePages(ProvincePage province) throws ServiceException {
        if (province == null) {
            return null;
        }
        return provincePageBOImpl.findProvincePages(province);
    }

}
